﻿using System.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThesisSystem.Models;
using System.Web.Mvc;
using ThesisSystem.DAL;
using System.Data;

namespace ThesisSystem.Controllers
{
    public class TestController : Controller
    {
        ThesisSystemEntities db = new ThesisSystemEntities();
        //
        // GET: /Test/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Desktop()
        {
            return View();
        }

        public ActionResult Menu()
        {
            var menus = db.SysMenus.ToList();
            return View(menus);
        }

        public ActionResult MT()
        {

            //添加
            var marking = new Marking();
            marking.Title = "title";
            marking.Description = "description";
            marking.MarkType = "hehe";
            marking.Targets = new List<Target>();
            marking.Targets.Add(db.Targets.Find(2));
            db.Markings.Add(marking);
            db.SaveChanges();
            return Content("succeed");
        }

        public ActionResult MTModify(int markingId,int targetID)
        {
            //删除
            var markingToUpdate = db.Markings
                            .Include(m=>m.Targets)
                            .Where(m=>m.MarkingID==markingId)
                            .Single();
            markingToUpdate.Targets = markingToUpdate.Targets.Where(t => t.TargetID != targetID).ToList();
            db.Entry(markingToUpdate).State = EntityState.Modified;
            db.SaveChanges();
            return Content("succeed");
        }
        public ActionResult MTTargetModify(int markingId, int targetNewID)
        {
            //修改,
            var markingToUpdate = db.Markings
                            .Include(m => m.Targets)
                            .Where(m => m.MarkingID == markingId)
                            .Single();
            var targetsToUpdate=new List<Target>();
            targetsToUpdate.Add(db.Targets.Find(targetNewID));
            markingToUpdate.Targets = targetsToUpdate;
            db.SaveChanges();
            return Content("succeed!");
        }

        public ActionResult T2M(int targetId)
        {
            var target = db.Targets
                .Include(t => t.Markings)
                .Where(t => t.TargetID == targetId)
                .Single();
            return View(target);
        }

        public ActionResult Rand()
        {
            Random rand = new Random();
            string s="";
            for (int i = 1; i < 1000; i++)
            {
                s = s + rand.Next(0, 2) ;
            }
            return Content(s);
        }
    }
}
